#include "dbsearchhelper.h"
#include "QString"
#include "iostream"
#include "vector"
#include "goodinfodao.h"
#include "goodinfo.h"
#include "QTimer"
#include "QDebug"
#include "mainwindow.h"

DBSearchHelper* DBSearchHelper::instance = NULL;


DBSearchHelper::DBSearchHelper():
    m_all_search_good_infos(NULL),
    m_cur_search_list(NULL),
    m_timer(NULL)
{
    m_last_search_id = QString("");
    m_all_search_good_infos = new std::vector<GoodInfo>();
    m_cur_search_list = new std::vector<QString>();
}

DBSearchHelper *DBSearchHelper::GetInstance()
{
    if(DBSearchHelper::instance == NULL)
    {
        DBSearchHelper::instance = new DBSearchHelper();
    }
    return DBSearchHelper::instance;
}

void DBSearchHelper::SearchGoodInfo(QString search_good_id)
{
    // 这里做一个搜索的缓冲池，主要解决重复搜索的问题
    // 第一次搜索了数据库以后的集合返回回来就在内存里面放着
    // 等再继续搜索的时候就直接遍历这个数据集合，从中筛选数据
    // 如果搜索值是空的，那么就清空列表

    // 如果搜索值是空的，那么就清空列表，从头再搜索过
    if(search_good_id.isNull() || search_good_id.isEmpty())
    {
        Clear();
    }
    else
    {
        // 第一次搜索，就从数据库中搜索数据并把所有数据缓存起来。
        // 如果是已经搜索过了，那么再在原有数据上进行筛选
        if(m_all_search_good_infos->size() <= 0)
        {
            FirstTimeSearchGoodInfo(search_good_id);
        }
        else if(m_last_search_id.size() > search_good_id.size())
        {
              RefreshSearch(search_good_id);
        }
        else
        {
            m_cur_search_list->clear();
            for(auto it = m_all_search_good_infos->begin();it != m_all_search_good_infos->end(); ++it)
            {
                QString search_str = search_good_id.contains("%")? search_good_id.replace("%",""):search_good_id;
                //qDebug()<< QString("DBSearchHelper::SearchGoodInfo search_good_id : %1   search_str:  %2").arg(search_good_id,search_str);
                if(it->good_id.size() >= search_str.size() && it->good_id.startsWith(search_str))
                {
                    m_cur_search_list->push_back(it->good_id);
                }
            }

            // 如果是没有要找的内容就重新搜索一次
            if(m_cur_search_list->size() <= 0)
            {
                RefreshSearch(search_good_id);
            }
        }
    }
}


void DBSearchHelper::RefreshSearch(QString search_good_id)
{
    qDebug() << QString("DBSearchHelper::RefreshSearch  search_good_id : %1").arg(search_good_id);
    Clear();
    if(!search_good_id.isNull() && !search_good_id.isEmpty())
    {
        SearchGoodInfo(search_good_id);
    }
}

void DBSearchHelper::Clear()
{
    m_last_search_id.clear();
    m_all_search_good_infos->clear();
    m_cur_search_list->clear();
}

// 第一次搜索设置延时搜索
// 如果打字很快，那么设置延时缓冲能够减少很多的开销
void DBSearchHelper::FirstTimeSearchGoodInfo(QString search_good_id)
{
    if(m_timer != NULL)
    {
        m_timer->stop();
        m_timer = NULL;
    }

    this->m_all_search_good_infos->clear();
    this->m_cur_search_list->clear();
    m_timer = new QTimer();
    QObject::connect(m_timer,&QTimer::timeout,[=](){
         auto list = GoodInfoDao::GetInstance()->Select(search_good_id);
         //qDebug() << QString("__DBSearchHelper::FirstTimeSearchGoodInfo  size : %1   search_good_id: %2").arg(QString::number(list.size()),search_good_id);
         for(auto info : list)
         {
              this->m_all_search_good_infos->push_back(info);
              this->m_cur_search_list->push_back(info.good_id);
         }
         this->m_timer->stop();
         this->m_timer = NULL;
         this->m_last_search_id = search_good_id;
         if(this->m_cur_search_list->size() > 0)
         {
            MainWindow::GetInstance()->SetSearchIdInputValue(this->m_cur_search_list);
         }
    });
    m_timer->start(0.5 * 1000);
}

GoodInfo *DBSearchHelper::GetGoodInfo(QString search_id)
{
    GoodInfo *good_info = NULL;
    for(auto it = m_all_search_good_infos->begin();it != m_all_search_good_infos->end(); ++it)
    {
        if(it->good_id.compare(search_id) == 0)
        {
            good_info = new GoodInfo();
            good_info->good_id = it->good_id;
            good_info->good_name = it->good_name;
            good_info->cost_price = it->cost_price;
            good_info->sale_price = it->sale_price;
            good_info->inventory = it->inventory;
            good_info->description = it->description;
            good_info ->img_name = it->img_name;
        }
    }
    return good_info;
}

std::vector<QString> *DBSearchHelper::GetSearchList()
{
    //qDebug() << QString("__DBSearchHelper::GetSearchList  size : %1").arg(QString::number(m_cur_search_list->size()));
    return m_cur_search_list;
}


